using Engine;
using Game;
using SCIENEW;

namespace Game {
    public class IECraftingRecipeSlotWidget : CraftingRecipeSlotWidget {
        public bool CustomLabel { get; set; } = false;

        public InteractableWidget m_clickableWidget;//可点击界面
        private RectangleWidget m_highlightedRect;
        private LabelWidget m_blockNameLabel;

        /// <summary>
        /// 是否被禁用
        /// </summary>
        public bool IsMouseHover { get => m_clickableWidget.IsMouseHover; }

        public IECraftingRecipeSlotWidget() : base() {
            m_clickableWidget = new InteractableWidget();
            Children.Add(m_clickableWidget);
            m_highlightedRect = new RectangleWidget { FillColor = new Engine.Color(30, 30, 30, 100), OutlineColor = new Engine.Color(48, 48, 48), OutlineThickness = 1f };
            Children.Add(m_highlightedRect);
            m_blockNameLabel = new LabelWidget { VerticalAlignment = WidgetAlignment.Near, HorizontalAlignment = WidgetAlignment.Center, FontScale = 0.5f, Margin = new Engine.Vector2(0, 5) };
            Children.Add(m_blockNameLabel);
        }

        public override void MeasureOverride(Vector2 parentAvailableSize) {
            m_blockIconWidget.IsVisible = false;
            int value = 0;
            if (!CustomLabel) m_labelWidget.IsVisible = false;
            if (!string.IsNullOrEmpty(m_ingredient)) {
                IECraftingRecipesManager.DecodeIEIngredient(m_ingredient, out string craftingId, out int? data);
                int[] array = IECraftingRecipesManager.FindBlocksByCraftingId(craftingId);
                if (array.Length != 0) {
                    int block = array[(int)(1.0 * Time.RealTime) % array.Length];
                    m_blockIconWidget.Value = value = m_ingredient.Contains('-') ? block : Terrain.MakeBlockValue(Terrain.ExtractContents(block), 0, data.GetValueOrDefault(0));//判断是否为工业的合成ID
                    m_blockIconWidget.Light = 15;
                    m_blockIconWidget.IsVisible = true;
                }
            }
            else if (m_resultValue != 0) {
                m_blockIconWidget.Value = value = m_resultValue;
                m_blockIconWidget.Light = 15;
                if (!CustomLabel) m_labelWidget.Text = m_resultCount.ToString();
                m_blockIconWidget.IsVisible = true;
                if (!CustomLabel) m_labelWidget.IsVisible = true;
            }

            m_highlightedRect.IsVisible = IsMouseHover;
            m_blockNameLabel.IsVisible = IsMouseHover;

            //显示名称
            if (IsMouseHover) {
                Block displayBlock = BlocksManager.Blocks[Terrain.ExtractContents(value)];
                if (displayBlock is AirBlock) {
                    m_blockNameLabel.Text = "";
                }
                else {
                    try {
                        m_blockNameLabel.Text = displayBlock.GetDisplayName(null, value); //由于自然子系统传入值是null，所以有些时候会引发空引用报错。有报错就采取最普通的获取名称办法
                    }
                    catch (Exception) {
                        m_blockNameLabel.Text = GetDisplayName(displayBlock, value);
                    }
                }
            }

            //切换界面
            if (m_clickableWidget.IsClicked) {
                List<CraftingRecipe> recipes = IECraftingRecipesManager.SearchForRecipe(value);
                int num = recipes.Count;
                IERecipaediaRecipesScreen recipaediaRecipesScreen = ScreensManager.FindScreen<IERecipaediaRecipesScreen>("RecipaediaRecipes");
                if (value != 0
                    && num != 0) {
                    recipaediaRecipesScreen.SwitchToNewRecipe(recipes, 0);
                    AudioManager.PlaySound("Audio/UI/ButtonClick", 1, 0, 0);
                }
                m_clickableWidget.IsClicked = false;
            }

            SetSize(parentAvailableSize);
        }

        public static string GetDisplayName(Block block, int value) {
            int data = Terrain.ExtractData(value);
            string bn = $"{block.GetType().Name}:{data}";
            if (LanguageControl.TryGetBlock(bn, "DisplayName", out var result)) {
                return result;
            }
            return "";
        }

        void SetSize(Vector2 parentAvailableSize) {
            Vector2 desiredSize = Vector2.Zero;
            if (Size.X >= 0f) {
                parentAvailableSize.X = MathUtils.Min(parentAvailableSize.X, Size.X);
            }
            if (Size.Y >= 0f) {
                parentAvailableSize.Y = MathUtils.Min(parentAvailableSize.Y, Size.Y);
            }
            foreach (Widget child in Children) {
                if (child.IsVisible) {
                    Vector2? widgetPosition = GetWidgetPosition(child);
                    Vector2 v = widgetPosition ?? Vector2.Zero;
                    child.Measure(Vector2.Max(parentAvailableSize - v - (2f * child.Margin), Vector2.Zero));
                    Vector2 vector = default;
                    vector.X = MathUtils.Max(desiredSize.X, v.X + child.ParentDesiredSize.X + (2f * child.Margin.X));
                    vector.Y = MathUtils.Max(desiredSize.Y, v.Y + child.ParentDesiredSize.Y + (2f * child.Margin.Y));
                    desiredSize = vector;
                }
            }
            if (Size.X >= 0f) {
                desiredSize.X = Size.X;
            }
            if (Size.Y >= 0f) {
                desiredSize.Y = Size.Y;
            }
            DesiredSize = desiredSize;
        }
    }
}